﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace DLL.Entities
{
    public class XMLHandler
    {
        public static bool WriteXML(object savedObj, string fileToSave, XmlSerializer serializer) 
        { 
            try 
            { 
                string dir = "..\\..\\..\\DLL\\XML\\" + fileToSave;
                TextWriter textWriter = new StreamWriter(fileToSave); 
                //TextWriter textWriter = new StreamWriter(dir);
                serializer.Serialize(textWriter, savedObj); 
                textWriter.Close(); 
                return true; 
            } 
            catch (Exception ex)
            { 
                UtilityEX.LogFile(ex.ToString()); 
                return false; 
            }
        }

        public static bool ReadXML(out object obj, string fileToRead, XmlSerializer serializer)
        {
            object recvedObj = new object();
            try
            {
                string dir = "..\\..\\..\\DLL\\XML\\" + fileToRead;
                TextReader xmlReader = new StreamReader(fileToRead);
                XmlReader reader = new XmlTextReader(dir);
                recvedObj = serializer.Deserialize(xmlReader);
                obj = serializer.Deserialize(reader);
                xmlReader.Close();
                return true;
            }
            catch (Exception ex)
            {
                UtilityEX.LogFile(ex.ToString());
                obj = null;
                return false;
            }

        }
    }
}
